define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap"
],function($,IO,bootbox,bootstrap){
    //设置判断按钮
    var gb_btn;
    var team_id;
    /**
     * 数据展示
     */
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '小组编号',
                dataIndex: 'name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].team_id+"</span>";
                }
            },
            {
                title: '小组名称',
                dataIndex: 'gender',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].team_name+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' class='user_detail' style='color: green;' team_id='"+data[datarow].team_id+"'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: red;' class='delete' team_id='"+data[datarow].team_id+"'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='/addNewMumber?team_id="+data[datarow].team_id+"'>新增成员</a></span>";
                }
            },

        ];


        //数组初始化
        function dataInit() {
            grid = new Grid("/ct/act/groupManagement", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            return "";
        }
        dataInit();

    });
    /**---------------------------------点击添加分类跳出模态框--------------------------------*/
    $("#addConsultingclassification").on('click',function(){
        $("#myModalLabel").text('添加小组');
        $("#myModal").modal('show');
        gb_btn = 0;
    });
    $(document).on('click','.user_detail',function(){
        team_id = $(this).attr('team_id');
        IO.get('/ct/act/team_detail',{team_id:team_id},function(d){
            $("#name").val(d.data.team_name);
        });
        $("#myModalLabel").text('编辑');
        gb_btn = 1;
        $("#myModal").modal('show');
    })
    //隐藏模态框后清除数据
    $('#myModal').on('hidden.bs.modal', function () {
        $('#name').val('');
    })
    /**---------------------------------点击模态框的增加判断操作类型--------------------------------*/
    $("#add").on("click",function(){
        var team_name = $("#name").val();
        if(team_name == ""){
            bootbox.alert("小组名称不能为空");
            return;
        }
        if(gb_btn == 0) {
            IO.post("/ct/act/team_add", {team_name: team_name}, function (d) {
                bootbox.alert(d.msg, function () {
                    if (d.code == 0) {
                        location.href = '/groupManagement';
                    }
                })
            });
        }else if(gb_btn == 1){
            var params={};
            params.team_name = team_name;
            params.team_id = team_id;
            IO.post("/ct/act/team_edit",params, function (d) {
                bootbox.alert(d.msg, function () {
                    if (d.code == 0) {
                        location.href = '/groupManagement';
                    }
                })
            });
        }else{
            bootbox.alert("哪里出错了?");
            return;
        }
    });
    /**---------------------------------删除操作--------------------------------*/
    $(document).on("click",".delete",function(){
        team_id=$(this).attr("team_id");
        if(team_id){
            bootbox.confirm("确定要删除该小组？",function(result){
                if(result) {
                    IO.get("/ct/act/team_delete",{team_id:team_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/groupManagement';
                            }
                        })
                    });
                }
            });
        }
    });
})